package myApp.DemoKey;

import android.app.Activity;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.inputmethodservice.KeyboardView.OnKeyboardActionListener;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;

//DIEU COMMIT
public class DemoKey extends Activity implements OnKeyListener,
		OnKeyboardActionListener {

	private static final String TAG = DemoKey.class.getName();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		KeyboardView keyboardView = (KeyboardView) findViewById(R.id.keyboardView);
		Keyboard keyboard = new Keyboard(this, R.xml.qwerty);
		keyboardView.setKeyboard(keyboard);
		keyboardView.setEnabled(true);
		keyboardView.setPreviewEnabled(true);
		keyboardView.setOnKeyListener(this);
		keyboardView.setOnKeyboardActionListener(this);

		View button = findViewById(R.id.button);
		button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				toggleKeyboardVisibility();
			}
		});
	}

	private void toggleKeyboardVisibility() {
		KeyboardView keyboardView = (KeyboardView) findViewById(R.id.keyboardView);
		int visibility = keyboardView.getVisibility();
		switch (visibility) {
		case View.VISIBLE:
			keyboardView.setVisibility(View.INVISIBLE);
			break;
		case View.GONE:
		case View.INVISIBLE:
			keyboardView.setVisibility(View.VISIBLE);
			break;
		}
	}

	

	@Override
	public void swipeUp() {
		Log.d(TAG, "swipeUp");
	}

	@Override
	public void swipeRight() {
		Log.d(TAG, "swipeRight");
	}

	@Override
	public void swipeLeft() {
		Log.d(TAG, "swipeLeft");
	}

	@Override
	public void swipeDown() {
		Log.d(TAG, "swipeDown");
	}

	@Override
	public void onText(CharSequence text) {
		Log.d(TAG, "onText? \"" + text + "\"");
	}

	@Override
	public void onRelease(int primaryCode) {
		Log.d(TAG, "onRelease? primaryCode=" + primaryCode);
	}

	@Override
	public void onPress(int primaryCode) {
		Log.d(TAG, "onPress? primaryCode=" + primaryCode);
	}

	@Override
	public void onKey(int primaryCode, int[] keyCodes) {
		Log.d(TAG, "onKey? primaryCode=" + primaryCode);
		int n1 = 0; // -1 count
		for (int keyCode : keyCodes) {
			if (keyCode == -1) {
				n1++;
				continue;
			}
			Log.v(TAG, "keyCode=" + keyCode);
		}
		Log.v(TAG, "keyCode=-1 *" + n1);
	}
	@Override
	public boolean onKey(View v, int keyCode, KeyEvent event) {
		Log.d(TAG, "onKey? keyCode=" + keyCode);
		return false;
	}
}